#include "adc_get.h"
#include "adConv1.h"
#include "adc_driver.h"

#define ADC_INSTANCE    0UL
#define ADC_CHN_1       12U
#define ADC_CHN_2       13U
#define ADC_CHN_3       14U
#define ADC_CHN_4       15U
#define ADC_VREFH       5.0f
#define ADC_VREFL       0.0f

float g_f_adcValue;

void ADC_Init(void)
{
    /* Enable the floating point unit */
    enableFPU();

    ADC_DRV_ConfigConverter(ADC_INSTANCE, &adConv1_ConvConfig0);
    ADC_DRV_AutoCalibration(ADC_INSTANCE);
}
 
float ADC_GET_EXT12(void)
{
	float 		 adcValue;
	uint16_t	 adcRawValue;
	uint16_t	 adcMax = 4096;
	/* Configure ADC channel and software trigger a conversion */
	ADC_DRV_ConfigChan(ADC_INSTANCE, 0U, &adConv1_ChnConfig0);
	/* Wait for the conversion to be done */
	ADC_DRV_WaitConvDone(ADC_INSTANCE);
	/* Store the channel result into a local variable */
	ADC_DRV_GetChanResult(ADC_INSTANCE, 0U, &adcRawValue);
 
	/* Process the result to get the value in volts */
	adcValue = ((float) adcRawValue / adcMax) * (ADC_VREFH - ADC_VREFL);
	return adcValue;
}
 
float ADC_GET_EXT13(void)
{
	float 		 adcValue;
	uint16_t	 adcRawValue;
	uint16_t	 adcMax = 4096;
	/* Configure ADC channel and software trigger a conversion */
	ADC_DRV_ConfigChan(ADC_INSTANCE, 1U, &adConv1_ChnConfig1);
	/* Wait for the conversion to be done */
	ADC_DRV_WaitConvDone(ADC_INSTANCE);
	/* Store the channel result into a local variable */
	ADC_DRV_GetChanResult(ADC_INSTANCE, 1U, &adcRawValue);
 
	/* Process the result to get the value in volts */
	adcValue = ((float) adcRawValue / adcMax) * (ADC_VREFH - ADC_VREFL);
	return adcValue;
}

float ADC_GET_EXT14(void)
{
	float 		 adcValue;
	uint16_t	 adcRawValue;
	uint16_t	 adcMax = 4096;
	/* Configure ADC channel and software trigger a conversion */
	ADC_DRV_ConfigChan(ADC_INSTANCE, 2U, &adConv1_ChnConfig2);
	/* Wait for the conversion to be done */
	ADC_DRV_WaitConvDone(ADC_INSTANCE);
	/* Store the channel result into a local variable */
	ADC_DRV_GetChanResult(ADC_INSTANCE, 2U, &adcRawValue);
 
	/* Process the result to get the value in volts */
	adcValue = ((float) adcRawValue / adcMax) * (ADC_VREFH - ADC_VREFL);
	return adcValue;
}
 
float ADC_GET_EXT15(void)
{
	float 		 adcValue;
	uint16_t	 adcRawValue;
	uint16_t	 adcMax = 4096;
	/* Configure ADC channel and software trigger a conversion */
	ADC_DRV_ConfigChan(ADC_INSTANCE, 3U, &adConv1_ChnConfig3);
	/* Wait for the conversion to be done */
	ADC_DRV_WaitConvDone(ADC_INSTANCE);
	/* Store the channel result into a local variable */
	ADC_DRV_GetChanResult(ADC_INSTANCE, 3U, &adcRawValue);
 
	/* Process the result to get the value in volts */
	adcValue = ((float) adcRawValue / adcMax) * (ADC_VREFH - ADC_VREFL);
	return adcValue;
}

 